package com.chuanyang.driverh.mainhome;


import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseTitleActivity;
import com.chuanyang.driverh.mainhome.adapter.GoodsForAllotSuccessAdapter;
import com.chuanyang.driverh.response2.ResultAllotGoodsSuccess;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.eventbus.EventBusUtil;
import com.chuanyang.driverh.utils.eventbus.FinishEvent;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;


public class CarPlanSuccessActivity extends BaseTitleActivity {
    private ImageView iv_header;
    private TextView tv_name;
    private TextView tv_car_band;
    private TextView tv_order_num;
    private TextView tv_complete;
    private TextView tv_goon_assign;
    private ListView lv_goods;
    private String waybilId;

    @Override
    protected int getContentResId() {
        return R.layout.home_act_assign_order_success;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        iv_header=(ImageView)findViewById(R.id.iv_header);
        tv_name=(TextView)findViewById(R.id.tv_name);
        tv_car_band=(TextView)findViewById(R.id.tv_car_band);
        tv_order_num=(TextView)findViewById(R.id.tv_order_num);
        tv_complete=(TextView)findViewById(R.id.tv_complete);
        tv_goon_assign=(TextView)findViewById(R.id.tv_goon_assign);
        lv_goods=(ListView)findViewById(R.id.lv_goods);

    }

    @Override
    protected void initData() {
        setBackImg();
        getTitleTextView().setText("派单成功");
        waybilId=getIntent().getStringExtra("data");
        getData();
    }

    @Override
    protected void initListener() {
        tv_complete.setOnClickListener(this);
        tv_goon_assign.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_complete:
                EventBusUtil.postEvent(new FinishEvent(1));
                finish();
                break;

            case  R.id.tv_goon_assign:
                finish();
                break;

        }
    }

    private void getData() {
        DialogUtil.showLoadingDialog(activityContext,"加载中...");
        Map<String,Object> params=new HashMap<String, Object>();
        params.put("driverId", SharedPrefUtil.getUserId());
//        params.put("page", mCurrentPage + "");
        params.put("waybillId", waybilId);

        MyHttpUtil.getWaybillInfoByWaybillId(activityContext,params, new MyCallback<ResultAllotGoodsSuccess>(activityContext,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activityContext, "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(ResultAllotGoodsSuccess result, int id) {
                if (result.getCode() == ConstUtil.OK) {
//                    setDataResult(result.getData().getData());
                    setListAndTop(result.getData());
                }else{
                    PrintUtil.toast(activityContext,result.getMsg());
                }
            }
        });
    }

    private void setListAndTop(ResultAllotGoodsSuccess.DataBean result) {


        try {
//        BitmapUtil.showImgForNetPath(result.getData().);
            tv_name.setText(result.getUsername());
            tv_car_band.setText(result.getPlateNumber());
            tv_order_num.setText(result.getWaybillNo());
            List<ResultAllotGoodsSuccess.DataBean.WaybillGoodsInfosBean> ss = result.getWaybillGoodsInfos();
            GoodsForAllotSuccessAdapter adapter=new GoodsForAllotSuccessAdapter(activityContext,ss);
            lv_goods.setAdapter(adapter);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
